package com.yh.action;

import com.opensymphony.xwork2.ModelDriven;
import com.yh.entity.Frm;
import com.yh.entity.User;
import com.yh.services.IUserService;
import com.yh.services.impl.UserServiceImpl;
import com.yh.util.BaseAction;
import lombok.Data;

import java.io.IOException;
import java.util.List;

@Data
public class UserAllAction extends BaseAction  implements ModelDriven<Frm> {
    private Frm frm=new Frm();
    private List<User> list;
    IUserService iUserService=new UserServiceImpl();

    public String show() throws IOException {
        list=iUserService.showAll();
        return "ok";

    }

    public String updf(){
        User user=new User();
        user.setCol_Id(Integer.parseInt(frm.getId()));
        User users=iUserService.getUserByIud(user);
        System.out.println(users.getCol_Name());
        if (users!=null){
            getSession().setAttribute("t",users);
            return "zhuan";
        }else {
            return "out";
        }
    }

    public String updl(){
        User user=new User();
        user.setCol_Id(Integer.parseInt(frm.getId()));
        user.setCol_Pwd(frm.getRepwd());
        int b=iUserService.updUser(user);
        if (b>0){
            return SUCCESS;
        }else{
            return "out";
        }
    }
    public String del(){
        User user=new User();
        user.setCol_Id(Integer.parseInt(frm.getId()));
        int b=iUserService.delUser(user);
        if (b>0){
            return SUCCESS;
        }else {
            return "out";
        }
    }

    public String add() throws IOException {
        User user=new User();
        user.setCol_Name(frm.getUname());
        user.setCol_Pwd(frm.getRepwd());

        User u= iUserService.getUserByIud(user);
        if (u==null){
            int b=iUserService.addUser(user);
            if (b>0){

                return SUCCESS;
            }else {
                getResponse().getWriter().print("alert('用户已存在')");
                return "out";
            }
        }else {
            getResponse().getWriter().print("alert('用户已存在')");
            return "out";
        }
    }

    @Override
    public Frm getModel() {
        return this.frm;
    }


}
